<template>
  <div class="sidebar" :class="{ 'collapsed': isCollapsed }">
    <div class="logo-container">
      <span v-if="!isCollapsed" class="title">{{ title }}</span>
    </div>
    
    <el-scrollbar>
      <el-menu
        :default-active="activeMenu"
        class="sidebar-menu"
        :collapse="isCollapsed"
        background-color="#001529"
        text-color="#fff"
        active-text-color="#ffd04b"
        router
      >
        <slot name="menu-items"></slot>
      </el-menu>
    </el-scrollbar>
    
    <div class="sidebar-footer" @click="toggleCollapse">
      <el-icon v-if="isCollapsed"><Expand /></el-icon>
      <el-icon v-else><Fold /></el-icon>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { Expand, Fold } from '@element-plus/icons-vue';

const props = defineProps({
  title: {
    type: String,
    default: '广告营销管理系统'
  },
  activeMenu: {
    type: String,
    required: true
  }
});

const isCollapsed = ref(false);
const toggleCollapse = () => {
  isCollapsed.value = !isCollapsed.value;
};

defineExpose({ isCollapsed });
</script>

<style scoped>
.sidebar {
  width: 220px;
  height: 100%;
  background-color: #001529;
  transition: all 0.3s;
  display: flex;
  flex-direction: column;
  position: relative;
  z-index: 1000;
}

.sidebar.collapsed {
  width: 40px; /* 只保留折叠按钮的宽度 */
}

.sidebar.collapsed .logo-container,
.sidebar.collapsed .el-scrollbar {
  display: none; /* 折叠时隐藏logo和菜单内容 */
}

.logo-container {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 16px;
  color: #fff;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.title {
  margin-left: 10px;
  font-size: 18px;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-menu {
  flex: 1;
  border-right: none;
}

.sidebar-menu:not(.el-menu--collapse) {
  width: 220px;
}

.sidebar-footer {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background-color: #002140;
  cursor: pointer;
  transition: all 0.3s;
}

.sidebar.collapsed .sidebar-footer {
  position: fixed;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 50px;
  border-radius: 0 4px 4px 0;
}

.sidebar-footer:hover {
  background-color: #003366;
}
</style>